package com.aaa.book_mall.dao.impl;

import com.aaa.book_mall.dao.UserDao;
import com.aaa.book_mall.entity.User;
import com.aaa.book_mall.util.JDBCUtil;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.stereotype.Repository;

import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

/**
 * @Description: 客户表对应的实现类
 * @ClassName: UserDaoImpl
 * @Author: fengyun
 * @Date: 2021/2/21 10:46
 * @Version: 1.0
 */
@Repository
public class UserDaoImpl implements UserDao {
    @Override
    public User getUserByUP(String username, String password) {
        String sql="select * from t_user where username=? and password=?";
        List<Map<String, Object>> maps = JDBCUtil.queryListMap(sql, username, password);
        if(maps.size()>0){
            User user = new User();
            Map<String, Object> map = maps.get(0);
            try {
                BeanUtils.populate(user,map);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
            return user;
        }
        return null;
    }

    @Override
    public int getUserByUsername(String username) {
        String sql="select * from t_user where username=?";
        List<List> lists = JDBCUtil.queryList(sql, username);
        int i=0;
        if(lists.size()>0){
            i=1;
            return i;
        }
        return i;
    }

    @Override
    public int insertUser(User user) {
        String sql="insert into t_user values(null,?,?,?,?,?)";
        int i = JDBCUtil.update(sql, user.getUsername(), user.getPassword(), user.getSex(), user.getPhone(), user.getBirthday());
        return i;
    }
}
